package com.cinsea.ext.cjspx;

import com.cinsea.common.ActionType;
import com.cinsea.common.BasePX;
import com.cinsea.common.Env;
import com.cinsea.common.JdbcDao;
import com.cinsea.directory.service.DirectoryService;
import com.cinsea.form.service.FormService;
import com.cinsea.form.service.FormfieldService;
import com.cinsea.interfaces.model.Cell;
import com.cinsea.interfaces.model.Dataset;
import com.cinsea.interfaces.workflow.RequestInfo;
import com.cinsea.interfaces.workflow.WorkflowServiceImpl;
import com.cinsea.util.StringUtil;
import com.cinsea.workflow.model.Wfnode;
import com.cinsea.workflow.service.WfdefineService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: xf
 * Date: 12-10-17
 * Time: 上午8:33
 */
public class DocApprovePX extends BasePX {
    public void before(Map context) {
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        String actiontype = StringUtil.null2String(context.get("actiontype"));
        Wfnode node = (Wfnode) context.get("wfnode");
        String processid = (String) context.get("processid");
        String dowid = (String) context.get("dowid");
        if (actiontype.equals(ActionType.EDIT_FORMPROCESS)) {
            String docstatus = (String) ((Map) context.get("parameters")).get("cs_document_docstatus");
            if (StringUtil.null2String(docstatus).equals("1")) {//非草稿
                List<Map> doclist = jdbcDao.queryForList("select docstatus from cs_document where processid=?", new Object[]{processid});
                if (doclist.size() > 0) {
                    Map values = doclist.get(0);
                    String _status = String.valueOf(values.get("docstatus"));
                    if (!_status.equals("1")) {//不能直接发布，必须审批
                        context.put("cannotrelease", "1");
                    }
                }
            }

        }

    }

    public void after(Map context) {
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        String actiontype = StringUtil.null2String(context.get("actiontype"));
        Wfnode node = (Wfnode) context.get("wfnode");
        String processid = (String) context.get("processid");
        String dowid = (String) context.get("dowid");
        if (actiontype.equals(ActionType.ADD_FORMPROCESS)) {
            String docstatus = (String) ((Map) context.get("parameters")).get("cs_document_docstatus");
            String title = (String) ((Map) context.get("parameters")).get("cs_document_subject");
            if (StringUtil.null2String(docstatus).equals("1")) {//非草稿

                DirectoryService directoryService = Env.getBean("directoryService");
                String workflowid = directoryService.get(dowid).getWorkflowid(); //文档审批流程id
                String fieldid = directoryService.get(dowid).getApprovedfield();//审批流程表单文档browser字段id
                if (!StringUtil.isEmpty(workflowid) && !StringUtil.isEmpty(fieldid)) {
                    WfdefineService wfdefineService = Env.getBean("wfdefineService");
                    FormService formService = Env.getBean("formService");
                    FormfieldService formfieldService = Env.getBean("formfieldService");
                    String formid = wfdefineService.get(workflowid).getFormid();
                    jdbcDao.update("update cs_document set docstatus=0 where processid=?", new Object[]{processid});//修改为草稿后发起审批
                    Dataset dataset = new Dataset();
                    List<Cell> mainlist = new ArrayList(); //主表单元格
                    Cell c1 = new Cell(); //doc id
                    c1.setId(fieldid);
                    c1.setValue(processid);
                    Cell c2 = new Cell(); //title
                    c2.setId(formService.get(formid).getTablename() + "_title");
                    c2.setValue(title);
                    mainlist.add(c1);
                    mainlist.add(c2);
                    dataset.setMaintable(mainlist);
                    Map content = new HashMap();
                    content.put("workflowid", workflowid);
                    content.put("dataset", dataset);
                    makeWorkflow(content);
                }
            }
        }
        if (actiontype.equals(ActionType.EDIT_FORMPROCESS)) {
            String docstatus = (String) ((Map) context.get("parameters")).get("cs_document_docstatus");
            String title = (String) ((Map) context.get("parameters")).get("cs_document_subject");
            if (StringUtil.null2String(docstatus).equals("1")) {//非草稿
                DirectoryService directoryService = Env.getBean("directoryService");
                String workflowid = directoryService.get(dowid).getWorkflowid(); //文档审批流程id
                String fieldid = directoryService.get(dowid).getApprovedfield();//审批流程表单文档browser字段id


                if (!StringUtil.isEmpty(workflowid) && !StringUtil.isEmpty(fieldid)) {
                    WfdefineService wfdefineService = Env.getBean("wfdefineService");
                    FormService formService = Env.getBean("formService");
                    FormfieldService formfieldService = Env.getBean("formfieldService");
                    String formid = wfdefineService.get(workflowid).getFormid();
                    List wfprocesslist = jdbcDao.queryForList("select * from " + formService.get(formid).getTablename() + " where " + formfieldService.get(fieldid).getFieldname() + "=?", new Object[]{processid});
                    if (wfprocesslist.size() == 0) {
                        jdbcDao.update("update cs_document set docstatus=0 where processid=?", new Object[]{processid});//修改为草稿后发起审批
                        Dataset dataset = new Dataset();
                        List<Cell> mainlist = new ArrayList(); //主表单元格
                        Cell c1 = new Cell(); //doc id
                        c1.setId(fieldid);
                        c1.setValue(processid);
                        Cell c2 = new Cell(); //title
                        c2.setId(formService.get(formid).getTablename() + "_title");
                        c2.setValue(title);
                        mainlist.add(c1);
                        mainlist.add(c2);
                        dataset.setMaintable(mainlist);
                        Map content = new HashMap();
                        content.put("workflowid", workflowid);
                        content.put("dataset", dataset);
                        makeWorkflow(content);
                    } else {
                        if (StringUtil.null2String(context.get("cannotrelease")).equals("1"))
                            jdbcDao.update("update cs_document set docstatus=0 where processid=?", new Object[]{processid});//修改为草稿后发起审批
                    }
                }
            }

        }
        if (actiontype.equals(ActionType.SUBMIT_WFPROCESS)) {//修改文档状态为正式
            List<Map<String, String>> directorylist = jdbcDao.queryForList("select approvedfield from cs_directory where workflowid=?", new Object[]{dowid});
            if (directorylist.size() > 0) {
                String fieldid = directorylist.get(0).get("approvedfield");
                if (!StringUtil.isEmpty(fieldid)) {
                    WfdefineService wfdefineService = Env.getBean("wfdefineService");
                    FormService formService = Env.getBean("formService");
                    FormfieldService formfieldService = Env.getBean("formfieldService");
                    String formid = wfdefineService.get(dowid).getFormid();
                    String fieldname = formfieldService.get(fieldid).getFieldname();
                    String sql = "update cs_document set docstatus=1 where id in(select " + fieldname + " from " + formService.get(formid).getTablename() + " where processid=?)";
                    jdbcDao.update(sql, new Object[]{processid});
                }
            }

        }
    }

    private String makeWorkflow(Map content) {
        WorkflowServiceImpl si = new WorkflowServiceImpl();
        RequestInfo formdata = new RequestInfo();
        formdata.setOperator(Env.getRemoteUser().getId());
        formdata.setTypeid((String) content.get("workflowid"));
        formdata.setData((Dataset) content.get("dataset"));
        String processid = si.createRequest(formdata);
        return processid;
    }
}
